/**
 * 项目批量删除接口测试用例
 *
 * Created by snowingsea on 2020/02/09.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const { statusKind } = require('./../../../../phoenix/models/projects');

module.exports = describe('/DELETE projects.json', function () {
  const url = '/projects.json';
  const administrator = new Administrator();
  let tester;
  const newUserIds = [];

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((res) => {
        tester = res;
        newUserIds.push(tester.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'delete');
  common.should.be.tester(uri, url, 'delete');

  // 正常删除
  it('it should DELETE projects', function (done) {
    let newProjects;
    let projectTotalNumber;
    let newProjectTotalNumber;
    const newProjectCount = 10;
    let newTester;
    const newTesterProjectCount = 3;
    administrator.createRandomTester()
      .then((user) => {
        newTester = user;
        newUserIds.push(newTester.userId);
        return newTester.createRandomProjects(newTesterProjectCount);
      })
      .then(() => tester.createRandomProjects(Math.floor(Math.random() * 5)))
      .then(() => tester.getProjectTotalNumber())
      .then((totalNumber) => {
        projectTotalNumber = totalNumber;
        // 随机添加项目
        return tester.createRandomProjects(newProjectCount);
      })
      .then((projects) => {
        newProjects = projects;
        return tester.getProjectTotalNumber();
      })
      .then((totalNumber) => {
        newProjectTotalNumber = totalNumber;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ projectIds: newProjects.map(({ projectId }) => projectId) })
          .end((err, res) => {
            res.should.have.status(204);
            tester.getProjectTotalNumber()
              .then((totalNumber) => {
                newProjectTotalNumber.should.be.eql(projectTotalNumber + newProjectCount);
                totalNumber.should.be.eql(projectTotalNumber);
                return newTester.getProjectTotalNumber();
              })
              .then((totalNumber) => {
                totalNumber.should.be.eql(newTesterProjectCount);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });

  // 反选删除
  it('it should delete projects except original projects', function (done) {
    let exceptProjects;
    let newTester;
    const newTesterProjectCount = 3;
    administrator.createRandomTester()
      .then((user) => {
        newTester = user;
        newUserIds.push(newTester.userId);
        return newTester.createRandomProjects(newTesterProjectCount);
      })
      .then(() => tester.createRandomProjects(Math.floor(Math.random() * 4) + 1))
      .then((projects) => {
        exceptProjects = projects;
        // 随机添加项目
        return tester.createRandomProjects();
      })
      .then(() => tester.login())
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ exceptProjectIds: exceptProjects.map(({ projectId }) => projectId) })
          .end((err, res) => {
            res.should.have.status(204);
            tester.getProjectTotalNumber()
              .then((totalNumber) => {
                totalNumber.should.be.eql(exceptProjects.length);
                return newTester.getProjectTotalNumber();
              })
              .then((totalNumber) => {
                totalNumber.should.be.eql(newTesterProjectCount);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });

  // 删除用户所有项目
  it('it should delete all projects', function (done) {
    let newTester;
    const newTesterProjectCount = 3;
    administrator.createRandomTester()
      .then((user) => {
        newTester = user;
        newUserIds.push(newTester.userId);
        return newTester.createRandomProjects(newTesterProjectCount);
      })
      .then(() => tester.createRandomProjects())
      .then(() => tester.login())
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            tester.getProjectTotalNumber()
              .then((totalNumber) => {
                totalNumber.should.be.eql(0);
                return newTester.getProjectTotalNumber();
              })
              .then((totalNumber) => {
                totalNumber.should.be.eql(newTesterProjectCount);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });

  // 删除冻结项目
  it('it should delete frozen projects except one', function (done) {
    const exceptProjectIds = [];
    let normalProjectCount;
    let newTester;
    const newTesterProjectCount = 3;
    administrator.createRandomTester()
      .then((user) => {
        newTester = user;
        newUserIds.push(newTester.userId);
        return newTester.createRandomProjects(newTesterProjectCount);
      })
      .then(() => tester.createRandomProject())
      .then((project) => {
        exceptProjectIds.push(project.projectId);
        return tester.freezeProject(project.projectId);
      })
      .then(() => tester.createRandomProjects())
      .then((projects) => tester.randomFreezeProjects(projects))
      .then(() => tester.getProjectTotalNumber({ status: statusKind.normal }))
      .then((number) => {
        normalProjectCount = number;
        return tester.login();
      })
      .then(() => {
        chai.request(uri)
          .delete(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query({ exceptProjectIds, status: [statusKind.frozen] })
          .end((err, res) => {
            res.should.have.status(204);
            tester.getProjectTotalNumber()
              .then((totalNumber) => {
                totalNumber.should.be.eql(normalProjectCount + 1);
                return newTester.getProjectTotalNumber();
              })
              .then((totalNumber) => {
                totalNumber.should.be.eql(newTesterProjectCount);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });
});
